package com.example.pwj.ffstudy.ui.gank.presenter;

import com.example.pwj.ffstudy.base.BaseResponse;
import com.example.pwj.ffstudy.bean.gank.GankBean;
import com.example.pwj.ffstudy.ui.gank.contract.GankContact;
import com.example.pwj.ffstudy.ui.gank.model.GankModel;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/8/28 16:04
 * description：
 */

public class GankPresenter implements GankContact.Presenter{
    private GankContact.View view;
    private GankModel model;
    private int PAGE= 1;

    @Inject
    public GankPresenter(GankModel model) {
        this.model = model;
    }

    @Override
    public void takeView(GankContact.View view) {
        this.view = view;
    }

    @Override
    public void dropView() {
        this.view = null;
    }

    @Override
    public void loadGankDatas(boolean isMore) {
        if (isMore){
            PAGE++;
        }else {
            PAGE = 1;
        }
        model.getGankDatas(10, PAGE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseResponse<GankBean>() {
                    @Override
                    public void onNext(GankBean o) {
                        view.loadGankDatas(o);
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.loadError(e.getLocalizedMessage());
                    }

                    @Override
                    public void onComplete() {
                       view.loadFinish();
                    }
                });
    }
}
